home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8839 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Combine zero-offset with unit-offset arrays ?
  5. Date: 04 Mar 1996 15:31:59 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Mar4083159@qcd.lanl.gov>
  8. References: <4hes0h$52qi@info4.rus.uni-stuttgart.de>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: Markus Heller's message of 4 Mar 1996 13:42:41 GMT
  13.  
  14. In article <4hes0h$52qi@info4.rus.uni-stuttgart.de>
  15. Markus Heller <Markus.Heller@studbox.uni-stuttgart.de> writes:
  16.  
  17. <snip>
  18. MH: I want to combine my code (using zero-offset arrays) with
  19. MH: code using unit-offset arrays.
  20.  
  21. Bad idea. C was not designed to handle non-zero offsets easily. 
  22.  
  23. MH: The FAQ - 2.16 mentions a method used "in old editions of
  24. MH: Numerical Recipes in C" that leads to undefined behavior.
  25. MH: Unfortunately there's no alternative mentioned.
  26.  
  27. Because there is no _good_ alternative. You can declare float a[11]
  28. and use a[1] through a[10] (and waste the a[0]).
  29.  
  30. MH: E.g.:
  31. MH: 
  32. MH: void somefunction( float fa[], int n);
  33. MH: 
  34. MH: int main(void)
  35. MH: {
  36. MH:  float a[10];  /* zero offset: a[0] .. a[9] */
  37. MH:  int n=10;
  38. MH: 
  39. MH:  somefunction( a-1, 10 ); /* method suggested by Numerical Recipes,
  40. MH:                              2nd edition */
  41.  
  42. For some reason, the authors of numerical recipes decided to
  43. transliterate their fortran programs into C. This is always a bad
  44. idea: just rewrite what you want in C. 
  45.  
  46. Numerical recipes in C reminds me of the German who could `bekam a
  47. cauliflower at the corner store'. Whatever it is, it is not C.
  48.  
  49. MH: }                               
  50. MH:  
  51. MH: void somefunction( float fa[], int n)
  52. MH: {
  53. MH:  /* do something with a, assuming unit-offset: a[1]..a[10] */
  54. MH: }
  55. MH: 
  56. MH: 
  57. MH: Which alternatives are possible to the above mentioned method ?
  58. MH: (Is the method still leasding to undefined behavior ?)
  59.  
  60. Yes. Decrementing beyond the beginning of an object leads to undefined
  61. behaviour.
  62.  
  63. Cheers
  64. Tanmoy
  65. --
  66. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  67. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  68. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  69. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  70. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  71. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  72.